package cn.joybreeze.controller;


import cn.joybreeze.common.result.R;
import cn.joybreeze.common.vo.UserRoleQueryVO;
import cn.joybreeze.model.UserRoleDO;
import cn.joybreeze.service.UserRoleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kjwaa
 * @since 2021-05-23
 */
@RestController
@RequestMapping("/user-role-do")
public class UserRoleController {

    @Autowired
    UserRoleService userRoleService;

    /**
     * 添加
     * @param userRoleQueryVO
     * @return
     */
    @PostMapping("/add")
    public R addUserRole(UserRoleQueryVO userRoleQueryVO){
        UserRoleDO userRoleDO = new UserRoleDO();
        BeanUtils.copyProperties(userRoleQueryVO, userRoleDO);
        boolean isSuccess = userRoleService.saveOrUpdate(userRoleDO);
        if(isSuccess) return R.ok();
        return R.error();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R deleteUserRoleById(@PathVariable int id){
        boolean isSuccess = userRoleService.removeById(id);
        if(isSuccess) return R.ok();
        return R.error();
    }

    /**
     * 更新
     * @param id
     * @param userRoleQueryVO
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateUserRoleById(@PathVariable int id, @RequestBody UserRoleQueryVO userRoleQueryVO){
        UserRoleDO userRoleDO = userRoleService.getById(id);
        BeanUtils.copyProperties(userRoleQueryVO, userRoleDO);
        boolean isSuccess = userRoleService.saveOrUpdate(userRoleDO);
        if(isSuccess) return R.ok();
        return R.error();
    }

    /**
     * 根据id获取记录
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public R getUserRoleById(@PathVariable int id){
        UserRoleDO userRoleDO = userRoleService.getById(id);
        if(userRoleDO == null) return R.error();
        return R.ok(userRoleDO);
    }

    /**
     * 用户角色列表分页
     * @param userRoleQueryVO
     * @return
     */
    @GetMapping("/list")
    public R getUserRoleList(UserRoleQueryVO userRoleQueryVO){
        Page<UserRoleDO> page = new Page<>();
        page.setCurrent(userRoleQueryVO.getCurrent());
        page.setSize(userRoleQueryVO.getSize());
        Page<UserRoleDO> doPage = userRoleService.page(page);
        return R.ok(doPage);
    }


}
